const express = require('express')
const app = express()
const moment = require('moment');
const fs = require('fs')
const port = 617

//time 用户调用接口时间
//ip 用户ip
//method 用户调用接口方法
//agent 用户浏览器信息
function Mylog(time, ip, method, agent) {
	//获取当天的日期生成一个txt文件
	let toDay = moment().format("YYYY-MM-DD");
	//如果没有叫当天日期的文件就会自动生成一个
	fs.appendFile(`log\\${toDay}.txt`,`${time}-${ip}-${method}-${agent}`+"\r\n", err => {
		console.log(err);
	})
}

//get调用方法
app.get('/GET', (request, response) => {
	//获取调用时的日期
	let nowDay = moment().format("YYYY-MM-DD HH:mm:ss");
	let userip = request.ip
	let userAgent = request.headers['user-agent']
	Mylog(nowDay, userip, "Get", userAgent)
})

//post调用方法
app.post('/POST', (request, response) => {
	let nowDay = moment().format("YYYY-MM-DD HH:mm:ss");
	let userip = request.ip
	let userAgent = request.headers['user-agent']
	Mylog(nowDay, userip, "POST", userAgent)
})

app.listen(port, () => console.log(`Example app listening on port port!`))